<?php
include_once("../root.inc.php");
include_once("../mysql/database_info.php");
include_once("../mysql/common.php");
include_once("../common.php");
// Read the notification from PayPal and create the acknowledgement response
$req = "cmd=_notify-validate";               // add 'cmd' to beginning of the acknowledgement you send back to PayPal
foreach ($_POST as $key => $value) {         // Loop through the notification NV pairs
	$value = urlencode(stripslashes($value));  // Encode the values
	$req .= "&$key=$value";                    // Add the NV pairs to the acknowledgement
}
$payment_date = $_POST['payment_date'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$custom = $_POST['custom'];
$payer_email = $_POST['payer_email'];
$payer_name = $_POST['first_name'] . " " . $_POST['last_name'];
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: ipnpb.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
fputs($fp, $header . $req);
$x = stream_get_contents($fp);
$y = stristr($x, "VERIFIED");
if ($y != FALSE) {  // Response is VERIFIED
  if ($payment_status == "Completed"){
  		$message = $payer_name . " has sent " . $payment_amount . " in " . $payment_currency . " on " . $payment_date . " for " . $item_name . ", item number: " . $item_number;
  		if (!mail("paypal@southhillcalvary.org", "Paypal Payment Received", $message)){
	  		mail("codys@southhillcalvary.org", "Error with paypal payment summery", "Failed to send email with paypal payment summery at ".date());
  		}
	}
}
else { 
	mail("codys@southhillcalvary.org", "Failure", "INVALID IPN in file:\n\n".$x);
}
fclose ($fp);  //close file pointer
?>